package zcj.stu.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.util.HashMap;

public class Result extends HashMap<String,Object> {
    public Result() {
        put("status", "success");
    }
    /*
     * 用于简便远程传输的繁琐的数据封装
     * */
    public Result setData(Object data){
        put("data",data);
        return this;
    }
    public<T> T getData(TypeReference<T> tTypeReference){
        Object data = get("data");
        String toJSON = JSON.toJSONString(data);
        T t = JSON.parseObject(toJSON, tTypeReference);
        return t;
    }
    public<T> T getData(String key,TypeReference<T> tTypeReference){
        Object data = get(key);
        String toJSON = JSON.toJSONString(data);
        T t = JSON.parseObject(toJSON, tTypeReference);
        return t;
    }
    public static Result error() {
        Result result = new Result();
        result.put("status","fail");
        return result;
    }
    public static Result error(String errMsg) {
        Result result = new Result();
        result.put("status","fail");
        result.put("data",errMsg);
        return result;
    }
    public static Result ok() {
        return new Result();
    }

    public static Result ok(Object data) {
        Result r = new Result();
        r.put("data",data);
        return r;
    }

    public Result put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
